package com.paobuji.aidebao.myapplication.adapter;

import android.content.Context;
import android.graphics.Color;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.Priority;
import com.bumptech.glide.load.engine.DiskCacheStrategy;
import com.paobuji.aidebao.myapplication.R;
import com.paobuji.aidebao.myapplication.bean.GetRankingList;
import com.paobuji.aidebao.myapplication.view.CircleImageView;

import java.util.List;
import java.util.Random;

/**
 * Created by lenovo on 2017/3/24.
 */

public class MyRankingListAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
    private Context context;
    private LayoutInflater inflater;
    private List<GetRankingList.DataBean.CollectionlistBean> list;
    private int[] colors;
    private Random random;

    public MyRankingListAdapter(Context context, List<GetRankingList.DataBean.CollectionlistBean> list) {
        this.context = context;
        this.list = list;
        random = new Random();
        colors = new int[]{Color.RED, Color.GREEN, Color.BLUE, Color.YELLOW};
        inflater = LayoutInflater.from(context);
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = inflater.inflate(R.layout.item_rankinglist, null);
        return new MyViewHolder(view);
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
        if(position == 0){
            setImg(holder,R.mipmap.champion);
            /*Glide.with(context)
                    // .load(mResultsPath.get(0))
                    .load(R.mipmap.worldrank_icon_goldcrown_me)
                    .asBitmap()
                    .skipMemoryCache(false) //跳过内存缓存
                    .diskCacheStrategy(DiskCacheStrategy.SOURCE)     //缓存设置
                    .priority(Priority.NORMAL)    //优先级  LOW     NORMAL      HIGH      IMMEDIATE
                    .placeholder(R.mipmap.ic_launcher)    //占位图
                    .error(R.mipmap.ic_launcher)
                    .into(((MyViewHolder) holder).rankingicon);*/
        }else if(position < 3){
            setImg(holder,R.mipmap.worldrank_icon_goldcrown_me);
            /*Glide.with(context)
                    // .load(mResultsPath.get(0))
                    .load(R.mipmap.worldrank_icon_goldcrown_me)
                    .asBitmap()
                    .skipMemoryCache(false) //跳过内存缓存
                    .diskCacheStrategy(DiskCacheStrategy.SOURCE)     //缓存设置
                    .priority(Priority.NORMAL)    //优先级  LOW     NORMAL      HIGH      IMMEDIATE
                    .placeholder(R.mipmap.ic_launcher)    //占位图
                    .error(R.mipmap.ic_launcher)
                    .into(((MyViewHolder) holder).rankingicon);*/
        }else{
            setImg(holder,R.mipmap.worldrank_icon_slivercrown_me);
            /*Glide.with(context)
                    // .load(mResultsPath.get(0))
                    .load(R.mipmap.worldrank_icon_slivercrown_me)
                    .asBitmap()
                    .skipMemoryCache(false) //跳过内存缓存
                    .diskCacheStrategy(DiskCacheStrategy.SOURCE)     //缓存设置
                    .priority(Priority.NORMAL)    //优先级  LOW     NORMAL      HIGH      IMMEDIATE
                    .placeholder(R.mipmap.ic_launcher)    //占位图
                    .error(R.mipmap.ic_launcher)
                    .into(((MyViewHolder) holder).rankingicon);*/
        }
        ((MyViewHolder) holder).num.setText("" + (position + 1));
        Glide.with(context)
                // .load(mResultsPath.get(0))
                .load(list.get(position).getUserImg())
                .asBitmap()
                .skipMemoryCache(false) //跳过内存缓存
                .diskCacheStrategy(DiskCacheStrategy.SOURCE)     //缓存设置
                .priority(Priority.NORMAL)    //优先级  LOW     NORMAL      HIGH      IMMEDIATE
                .placeholder(R.mipmap.zhanwei)    //占位图
                .error(R.mipmap.zhanwei)
                .into(((MyViewHolder) holder).circleImageView);
        ((MyViewHolder) holder).username.setText(list.get(position).getUsername());
        ((MyViewHolder) holder).allDis.setText( context.getResources().getString(R.string.alDis) + " :" + list.get(position).getAllDis());
        ((MyViewHolder) holder).allCor.setText(context.getResources().getString(R.string.calorie) + " :" + list.get(position).getAllCir());
        ((MyViewHolder) holder).allTime.setText(context.getResources().getString(R.string.time) + " :" + list.get(position).getAllTime());
    }

    @Override
    public int getItemCount() {
        return list.size();
    }

    class MyViewHolder extends RecyclerView.ViewHolder {

        private ImageView rankingicon;
        private TextView num;
        private CircleImageView circleImageView;
        private TextView username;
        private TextView allDis;
        private TextView allCor;
        private TextView allTime;

        public MyViewHolder(View itemView) {
            super(itemView);

            rankingicon = (ImageView) itemView.findViewById(R.id.rankingicon);
            num = (TextView) itemView.findViewById(R.id.num);
            circleImageView = (CircleImageView) itemView.findViewById(R.id.myUserImage);
            username = (TextView) itemView.findViewById(R.id.userName);
            allDis = (TextView) itemView.findViewById(R.id.allDis);
            allCor = (TextView) itemView.findViewById(R.id.allCor);
            allTime = (TextView) itemView.findViewById(R.id.allTime);
        }
    }

    public void setImg(RecyclerView.ViewHolder holder,Integer img){
        Glide.with(context)
                // .load(mResultsPath.get(0))
                .load(img)
                .asBitmap()
                .skipMemoryCache(false) //跳过内存缓存
                .diskCacheStrategy(DiskCacheStrategy.SOURCE)     //缓存设置
                .priority(Priority.NORMAL)    //优先级  LOW     NORMAL      HIGH      IMMEDIATE
                .placeholder(R.mipmap.zhanwei)    //占位图
                .error(R.mipmap.zhanwei)
                .into(((MyViewHolder) holder).rankingicon);
    }
}
